package com.wjj.happytime.fragment.happy_frg;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.wjj.happytime.R;
import com.wjj.happytime.adapter.happy_frg_adapter.ItemAdapter;
import com.wjj.happytime.application.BaseApplication;
import com.wjj.happytime.entity.happy_entity.Entity;

import com.wjj.happytime.my_interface.HttpService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommonHappyFrg extends Fragment implements Callback<Entity>, OnRefreshListener {
    private int page=1;
    private int count=30;
    private RecyclerView recycler;
    private ItemAdapter adapter;
    private boolean isLoading;
    public String type;
    public SwipeRefreshLayout swip;
    public HttpService service;


    public CommonHappyFrg() {
        // Required empty public constructor
    }

    public static CommonHappyFrg getInstance(String type) {
        CommonHappyFrg fragment = new CommonHappyFrg();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_common_happy_frg, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final BaseApplication application = (BaseApplication) getActivity().getApplication();
        //获取从activity传过来的type
        type = getArguments().getString("type");
        service = application.getService();
        service.getItems(type,page,count).enqueue(this);
        swip = ((SwipeRefreshLayout) view.findViewById(R.id.swip_happy));
        //下拉刷新监听
        swip.setOnRefreshListener(this);
        recycler = ((RecyclerView) view.findViewById(R.id.recycler_common_happy_id));
        //适配器
        adapter = new ItemAdapter(getContext());
        //绑定适配器
        recycler.setAdapter(adapter);
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layout = (LinearLayoutManager) recyclerView.getLayoutManager();
                //总条目
                int totalCount = layout.getItemCount();
                int childCount = layout.getChildCount();
                int firstVisibleItemPosition = layout.findFirstVisibleItemPosition();
                //最后一个可见的条目位置
                int lastVisibleItemPosition = layout.findLastVisibleItemPosition();
                if (!isLoading&&lastVisibleItemPosition+1==totalCount) {
                    isLoading=true;
                    service.getItems(type,++page,count).enqueue(CommonHappyFrg.this);
                }



            }
        });



    }

    @Override
    public void onResponse(Call<Entity> call, Response<Entity> response) {
        if (swip!=null) {
            swip.setRefreshing(false);
        }
        JLog.e("第"+page+"页！");
        JLog.e("http://m2.qiushibaike.com/article/list/"+type+"?page="+page+"&count=30");
        adapter.addAll(response.body().getItems());
        isLoading=false;
    }

    @Override
    public void onFailure(Call<Entity> call, Throwable t) {
        if (swip!=null) {
            swip.setRefreshing(false);
        }
        isLoading=false;
        Toast.makeText(getContext(), "下载错误", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onPause() {
        super.onPause();
        if (adapter!=null) {
//            adapter.releaseAllVideos();
        }
    }

    @Override
    public void onRefresh() {
        if (adapter!=null) {
            //防止下拉刷新的同时上拉加载数据
            isLoading=true;
            adapter.cleanData();
            page=1;
            service.getItems(type,page,count).enqueue(this);
        }

    }
}
